from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import excel_info_sql
import biz_mysql

list1 = excel_info_sql.getConditions()


def Inovel_test():
    driver = webdriver.Chrome()
    driver.get("https://www.ziread.cn/admin/index/login?url=%2Fadmin")
    driver.maximize_window()
    driver.implicitly_wait(20)

    if "" in list1[0]["Platform"]:
        username = ""
    elif "" in list1[0]["Platform"]:
        username = ""
    elif "" in list1[0]["Platform"]:
        username = ""
    elif "" in list1[0]["Platform"]:
        username = ""
    else:
        username = ""
    # 定位账号
    driver.find_element_by_name('username').clear()
    driver.find_element_by_name('username').send_keys('{0}'.format(username))
    # 定位密码
    driver.find_element_by_name('password').clear()
    driver.find_element_by_name('password').send_keys('{0}'.format(''))
    # 点击登录
    driver.find_element_by_xpath('//div[5]/button').click()
    # 点击小说推广
    try:
        time.sleep(1)
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//section/ul/li[4]/a'))
        )
        driver.find_element_by_xpath('//section/ul/li[4]/a').click()
    except Exception:
        print("点击小说推广")
    # 小说中心：
    try:
        time.sleep(0.5)
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//li[4]//li[1]//span[1]'))
        )
        driver.find_element_by_xpath('//li[4]//li[1]//span[1]').click()
    except Exception:
        print("小说中心")
    for i in range(0, len(list1)):
        # 小说名字
        Novel_name = list1[i]["Novel_name"]
        # 日期
        insert_time = list1[i]["date_time"]
        # 章节
        Chapter = list1[i]["Chapter"]
        # 推广名称
        Extension_name = list1[i]["Extension_name"]
        # 平台名称
        Platform = list1[i]["Platform"]
        # 点击名称
        try:
            time.sleep(0.5)
            driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="con_375"]/iframe'))
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//*[@id="name"]'))
            )
            driver.find_element_by_xpath('//*[@id="name"]').clear()
            driver.find_element_by_xpath('//*[@id="name"]').send_keys(Novel_name)
        except Exception:
            print("点击名称")



        # 提交
        try:
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//div[9]//button[1]'))
            )
            driver.find_element_by_xpath('//div[9]//button[1]').click()
        except Exception:
            print("提交")

        # 推广链接
        try:
            time.sleep(0.5)
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//td[8]/a'))
            )
            driver.find_element_by_xpath('//td[8]/a').click()
        except Exception:
            print("无推广链接")

        # 获取第一个推广
        try:

            driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="layui-layer-iframe{0}"]'.format(i + 1)))
            driver.find_element_by_xpath('//tr[{0}]//a[4]'.format(Chapter)).click()
        except Exception:
            print("获取推广链接")

        # 渠道mingcheng：
        try:

            driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="layui-layer-iframe1"]'))
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//*[@id="c-name"]'))
            )
            driver.find_element_by_xpath('//*[@id="c-name"]').clear()
            driver.find_element_by_xpath('//*[@id="c-name"]').send_keys('{0}'.format(str(insert_time) + Extension_name))
        except Exception:
            print("渠道mingcheng")
        driver.find_element_by_xpath('//*[@id="c-guide_chapter_idx"]').send_keys(3)

        # 确认
        try:
            
            driver.find_element_by_xpath('//button[text()="确定"]').click()
            time.sleep(1.5)
 
            url = driver.find_element_by_xpath('//*[@id="add-dialog"]//div[1]/div[2]/div[2]').text
            print(url)
            # 数据入库
            biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name, Chapter=Chapter,
                                         datetime=str(insert_time), Extension_name=Extension_name)
        except Exception:
            print("确认出错")
        # 退出当前iframe
        driver.switch_to.default_content()
        # 取消
        try:
            driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="con_375"]/iframe'))
            driver.find_element_by_xpath('//*[@id="layui-layer{0}"]/span[1]/a[3]'.format(i + 1)).click()
        except Exception:
            print("取消1")
        driver.switch_to.default_content()
    driver.quit()


if __name__ == "__main__":
    Inovel_test()
